var assert = require('assert');
var logger = using('easynode.framework.Logger').forFile(__filename);
var GenericObject = using('easynode.GenericObject');
var ActionResult = using('easynode.framework.mvc.ActionResult');
var IRouteFilter = using('easynode.framework.server.http.IRouteFilter');
var md5 = require('md5');
var randomstring=require('randomstring');

(function () {
        const TOKEN = EasyNode.config('wx.auth.token','123456');
        const WHITELIST = [
        ];

        /**
         * Class PrivilegeFilter
         *
         * @class szzh.motor.console.controllers.filters.PrivilegeFilter
         * @extends easynode.GenericObject
         * @since 0.1.0
         * @author zlbbq
         * */
        class PrivilegeFilter extends IRouteFilter {
                /**
                 * 构造函数。
                 *
                 * @method 构造函数
                 * @since 0.1.0
                 * @author zlbbq
                 * */
                constructor() {
                        super();
                        this.wxOauth=null;
                        //调用super()后再定义子类成员。
                }

                filter (ctx, next) {
                        var me = this;
                        return function * () {
                                EasyNode.DEBUG && logger.debug(`checking privilege [${ctx.routeId}][${JSON.stringify(ctx.session.USER)}]`);

                         /*       var code=this.p('code');
                                if(code) //oauth2 跳回，根据返回的code  查询出用户的openid
                                {
                                        logger.error('get a code '+code);
                                        var token=yield me.wxOauth.getAccessToken(code);
                                        ctx.session.openid=token.data.openid;
                                }


                                if(ctx.session.openid)
                                {

                                        return yield next;
                                }
                                else
                                {
                                      //  var redirectUrl = ctx.protocol + '://' + ctx.get('host') + ctx.originalUrl;
                                        var redirectUrl = ctx.protocol + '://183.131.76.67:10000/wx/test.html';

                                        var state = md5(Date.now().toString() + randomstring.generate(4).toString());
                                        var snsapiType = 'snsapi_base';
                                        var authorizeUrl = me.wxOauth.getAuthorizeURL(redirectUrl, state,snsapiType);
                                        logger.error('redirectUrl:'+redirectUrl);

                                        logger.error('authorizeUrl:'+authorizeUrl);

                                        return  ctx.redirect(authorizeUrl);

                                }*/


                                var fn = me[ctx.routeId];
                                if(typeof fn == 'function') {
                                        var ret  = fn.call(me, ctx);
                                        if(ret) {
                                                return ret;
                                        }
                                }
                                return yield next;
                        };
                }

                getClassName() {
                        return EasyNode.namespace(__filename);
                }
        }

        module.exports = PrivilegeFilter;
})();